import router from './router/router';
import store from './store';
import {
    getToken,
} from '@/util/auth';

import {
    setTitle,
} from '@/util/util';
import {
    validatenull,
} from '@/util/validate';
import {
    asyncRouterMap,
} from '@/router/router';
// 检测token


router.addRoutes(asyncRouterMap); // 动态添加可访问路由表
router.beforeEach((to, from, next) => {
  router.hisPath = from;
  const website = store.getters.website;
  const token = store.getters.token;
  const isWuye = store.getters.isWuye;
  const community = store.getters.community;
    // 白名单直接过滤
  if (website.list.indexOf(to.path) !== -1) {
      next();
      return;
    }
    // 登陆页面判断是否含有token，
  if ((website.whiteList.indexOf(to.path) !== -1) && !token) {
      next();
      return;
    } else if (website.whiteList.indexOf(to.path) !== -1) {
      if (validatenull(community) && isWuye) { // 物业没有认证去授权页
          next(website.rolesPath);
        } else if (validatenull(community)) { // 业主没有认证去授权页
          next(website.authPath);
        } else {
            // 如果认证去主页
          next(website.indexPath);
        }
    }
  if (token) {
        // 其他页面如果有token直接过，没有的跳转授权页
      if (validatenull(community) && isWuye) {
          next(website.rolesPath);
        } else if (validatenull(community)) {
          next(website.authPath);
        }
      next();
    } else {
        // 没有token全部去登陆页
      next(website.loginPath);
    }
});
